set maxvar 120000
local saiaintfolder = "$path_pch/Data/intermediate/Saia"
pause on
	
local rstart = 0
local rend = 100
local num_sim = `rend'-`rstart'+1


// Prepare file to save
/* pval coefficient se for each column */
clear all
set obs `num_sim'
gen RInum = .
forv col = 3(3)6 { // Column of Saia's Table 4 that we're replicating with fake seating
	gen c`col'_coef = .
	gen c`col'_se = .
	gen c`col'_p = .
}
save "`saiaintfolder'/saia_simul_results", replace

// saia_simul_vote, sia_simul_vote_`s': prepared from saia_simulation.do
// Save them in RAM for faster loading
use "`saiaintfolder'/saia_simul_vote.dta", clear
tempfile saia_simul_vote
save `saia_simul_vote'

foreach s in U L F B {
	use "`saiaintfolder'/saia_simul_vote_`s'.dta", clear
	tempfile saia_simul_vote_`s'
	save `saia_simul_vote_`s''
}

// Run regression iteratively, save results
qui {
local kk = 1
forv r = `rstart'/`rend' {
	
	noisily dis "RI`r' start: $S_TIME"

	// Get neighbor info from here for specific RI
	/* ?_MP_id ?_party_id */
	use "$path_pch/Data/intermediate/Seating/seating_MP_frontback.dta", clear
	keep MP_id session_id party_id ?_MP_idRI`r' ?_party_idRI`r' seat_initRI`r'
	foreach var of varlist *RI`r' {
		local newname = subinstr("`var'","RI`r'","",1)
		ren `var' `newname'
	}
	ren seat_init seat
	// Control: proportion of other party peers
	g num_peers = (U_MP_id!=.) + (L_MP_id!=.) + (F_MP_id!=.) + (B_MP_id!=.)
	g num_othparty_peers = (U_party_id!=party_id & U_MP_id!=. & party_id!=.) + (L_party_id!=party_id & L_MP_id!=. & party_id!=.) + ///
		(F_party_id!=party_id & F_MP_id!=. & party_id!=.) + (B_party_id!=party_id & B_MP_id!=. & party_id!=.)
	g prop_othparty_peers = num_othparty_peers/num_peers
	assert prop_othparty_peers>=0 & prop_othparty_peers<=1
	// FE: PartyXSession
	egen PartyXSession = group(party_id session_id)
	egen MPXSession = group(MP_id session_id)
	drop num_peers num_othparty_peers
	tempfile neighborInfo
	save `neighborInfo'

	// Bring original vote data, get neighbor info
	use `saia_simul_vote', replace
	merge n:1 MP_id session_id using `neighborInfo', keep(3) nogen //12 MPs never appearing in voting data

	// Get neighbor vote, neighbor's party leader vote from saved files.
	foreach s in U L F B {
		merge n:1 vote_id `s'_MP_id using `saia_simul_vote_`s'', keep(1 3) nogen
	}

	// Dependent variable
	g non_compliance = vote!=chairman_vote if vote!="" & !inlist(chairman_vote,"","unknown")

	// Treatment (divergent_peers)
	g denom = (U_vote!="") + (L_vote!="") + (F_vote!="") + (B_vote!="") if chairman_vote!=""
	g divergent_peers = (U_vote!=chairman_vote & U_vote!="") + (L_vote!=chairman_vote & L_vote!="") + ///
		(F_vote!=chairman_vote & F_vote!="") + (B_vote!=chairman_vote & B_vote!="") if chairman_vote!=""
	replace divergent_peers = divergent_peers/denom

	// Instrument (divergent_peers_iv)
	g divergent_peers_iv = (U_chairman_vote!=chairman_vote & U_chairman_vote!="") + (L_chairman_vote!=chairman_vote & L_chairman_vote!="") + ///
		(F_chairman_vote!=chairman_vote & F_chairman_vote!="") + (B_chairman_vote!=chairman_vote & B_chairman_vote!="") if chairman_vote!=""
	replace divergent_peers_iv = divergent_peers_iv/denom
	drop denom			
	
	// Sample restriction
	g sample1 = !mi(non_compliance) & (MP_id != chairman_id)
	g sample2 = sample1 & !inlist(chairman_id,U_MP_id,L_MP_id,F_MP_id,B_MP_id)
	g sample3 = sample2
	forv k = 1/19 {
		replace sample3 = 0 if !mi(party`k'_chairman_id) & inlist(party`k'_chairman_id,U_MP_id,L_MP_id,F_MP_id,B_MP_id)
	}	
	
	// col. 3: 2SLS with controls	
	local col = 3
	xi: reghdfe non_compliance i.seat i.PartyXSession prop_othparty_peers ///
		(divergent_peers = divergent_peers_iv) if sample3, ///
		cluster(MPXSession) absorb(MP_id vote_id) old
	local c`col'_coef = _b[divergent_peers]
	local c`col'_se = _se[divergent_peers]
	test divergent_peers = 0
	local c`col'_p = r(p)
	
	// col. 6: 2SLS with controls, only when 100% othparty peers
	local col = 6
	xi: reghdfe non_compliance i.seat i.PartyXSession ///
		(divergent_peers = divergent_peers_iv) if sample3 & (prop_othparty_peers==1), ///
		cluster(MPXSession) absorb(MP_id vote_id) old
	local c`col'_coef = _b[divergent_peers]
	local c`col'_se = _se[divergent_peers]
	test divergent_peers = 0
	local c`col'_p = r(p)
	
	// Save results
	use "`saiaintfolder'/saia_simul_results.dta", clear
	replace RInum = `r' in `kk'
	forv col = 3(3)6 {
		replace c`col'_coef = `c`col'_coef' in `kk'
		replace c`col'_se = `c`col'_se' in `kk'
		replace c`col'_p = `c`col'_p' in `kk'
	}
	local kk = `kk'+1
	
	save "`saiaintfolder'/saia_simul_results.dta", replace
	
	
	noisily dis "RI`r' end: $S_TIME"
}
}


